package com.woniu.his.sms.app.apiImpl;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.his.result.CommonResultCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.exception.HisException;
import com.woniu.his.result.Result;
import com.woniu.his.sms.client.SmsStaffClient;
import com.woniu.his.sms.client.param.CreateStafffParam;
import com.woniu.his.sms.client.param.SmsSkdDocParam;
import com.woniu.his.sms.client.param.StaffLoginParam;
import com.woniu.his.sms.client.param.StaffPageParam;
import com.woniu.his.sms.client.param.StaffUpdateParam;
import com.woniu.his.sms.client.result.*;
import com.woniu.his.sms.client.param.StaffPwdParam;
import com.woniu.his.sms.infra.model.Dept;
import com.woniu.his.sms.infra.model.RegistrationRank;
import com.woniu.his.sms.infra.model.Role;
import com.woniu.his.sms.service.*;
import com.woniu.his.sms.service.dto.*;
import com.woniu.his.sms.service.param.PageStaffServerParam;
import com.woniu.his.sms.service.param.StaffLoginServerParam;
import com.woniu.his.sms.service.param.StaffUpdateServerParam;
import com.woniu.his.sms.util.JwtUtil;
import com.woniu.his.sms.common.SmsResultCode;
import com.woniu.his.sms.infra.model.Staff;
import com.woniu.his.sms.service.param.SmsSkdDocServiceParam;
import com.woniu.his.sms.service.param.StaffPwdSeParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("sms")
@RestController
public class SmsStaffApiImpl implements SmsStaffClient {


    @Resource
    private StaffService staffService;

    @Resource
    private RegistrationRankService registrationRankService;

    @Resource
    private DeptService deptService;

    @Resource
    private RoleService roleService;

    /**
     * 登录返回token
     *
     * @param loginParam
     * @return
     */
    @Override
    public Result<String> login(StaffLoginParam loginParam) {
        //打印信息 校验参数
        log.info("用户登录:{}", loginParam);
        StaffLoginServerParam serverParam = BeanUtil.copyProperties(loginParam, StaffLoginServerParam.class);
        Map map = staffService.login(serverParam);
        String token = JwtUtil.create(map);
        log.info("登录成功的token:{}", token);
        return Result.getInstance(String.class).setData(token);
    }

    @Override
    public Result<List<StaffInfoResult>> findAllStaffByDeptId(Integer deptId) {

        log.info("科室ID:{}",deptId);
        List<StaffInfoDto> list=staffService.findAllStaffByDeptId(deptId);
        List<StaffInfoResult> results=BeanUtil.copyToList(list,StaffInfoResult.class);
        return Result.getInstanceList(StaffInfoResult.class).setData(results);
    }


    /**
     * 分页模糊查询
     *
     * @return
     */
    @Override
    public Result<PageStaffResult> QueryPageStaff(StaffPageParam pageParam) {
        //打印信息
        log.info("模糊查询:{}", pageParam);
        //转换参数
        PageStaffServerParam param = BeanUtil.copyProperties(pageParam, PageStaffServerParam.class);
        PageStaffDto pageStaffDto = staffService.QueryPageStaff(param);
        PageStaffResult pageStaffResult = BeanUtil.copyProperties(pageStaffDto, PageStaffResult.class);
        List<StaffAllResult> list = BeanUtil.copyToList(pageStaffDto.getStaffList(), StaffAllResult.class);
        pageStaffResult.setStaffList(list);
        return Result.getInstance(PageStaffResult.class).setData(pageStaffResult);
    }

    /**
     * 查询全部用户
     *
     * @return
     */
    @Override
    public Result<List<StaffAllResult>> QueryAll() {
        log.info("查询所有所有用户:{}");
        List<StaffAllDto> staffList = staffService.queryAll();
        List<StaffAllResult> list = BeanUtil.copyToList(staffList, StaffAllResult.class);

        return Result.getInstanceList(StaffAllResult.class).setData(list);
    }
    /**
     * 修改用户信息
     *
     * @param param
     * @return
     */
    @Override
    public Result UpdateStaff(StaffUpdateParam param) {
        //打印用户信息且参数校验
        log.info("修改用户信息:{}", param);
        StaffUpdateServerParam updateServerParam = BeanUtil.copyProperties(param, StaffUpdateServerParam.class);
        Integer i = staffService.UpdateStaff(updateServerParam);
        if (i != 1) return Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("修改失败信息有误");
        return Result.getInstance();
    }


    /**
     * @Description: 查询上班医生
     * @Param:
     * @return: dx
     * @Date: 2022/11/26
     */
    @Override
    public Result<List<StaffResult>> listDocBySkd(SmsSkdDocParam smsSkdDocParam) {
        log.info("查询上班医生信息:{}", smsSkdDocParam);
        SmsSkdDocServiceParam smsSkdDocServiceParam = BeanUtil.copyProperties(smsSkdDocParam, SmsSkdDocServiceParam.class);
        List<StaffServiceDto> listDocBySkd = staffService.listDocBySkd(smsSkdDocServiceParam);
        List<StaffResult> staffResults = BeanUtil.copyToList(listDocBySkd, StaffResult.class);
        return Result.getInstanceList(StaffResult.class).setData(staffResults).setMsg("查找上班医生成功");

    }


    /**
     * @Description: 根据登录用户id获取登录用户
     * @Param: dx
     * @return:
     * @Date: 2022/11/26
     */
    @Override
    public Result<LoginStaffResult> findLoginStaffById(Integer staffId) {

        log.info("用户id:{}", staffId);
        loginStaffServiceDto loginStaffById = staffService.findLoginStaffById(staffId);
        LoginStaffResult loginStaffResult = BeanUtil.copyProperties(loginStaffById, LoginStaffResult.class);
        return Result.getInstance(LoginStaffResult.class).setData(loginStaffResult).setMsg("查找成功");
    }

    /**
     * @Description: 根据用户名密码查找用户
     * @Param:
     * @return: dx
     * @Date: 2022/11/26
     */
    @Override
    public Result<LoginStaffResult> findStaffByPwd(@RequestBody StaffPwdParam staffPwdParam){
        log.info("用户信息:{}", staffPwdParam);
        StaffPwdSeParam staffPwdSeParam = BeanUtil.copyProperties(staffPwdParam, StaffPwdSeParam.class);
        loginStaffServiceDto loginStaffResult =  staffService.findStaffByPwd(staffPwdSeParam);
        LoginStaffResult loginStaffResult1 = BeanUtil.copyProperties(loginStaffResult, LoginStaffResult.class);
        return Result.getInstance(LoginStaffResult.class).setData(loginStaffResult1).setMsg("查找成功");
    }


    /**
     * @Description: 根据用ids 删除员工用户
     * @Param: dx
     * @return:
     * @Date: 2022/11/26
     */
    @Override
    public Result deleteStaff(List<Long> ids) {
        log.info("删除员工用户id:{}", ids);
        return staffService.deleteStaff(ids) ? Result.getInstance().setMsg("删除成功") : Result.getInstance().setMsg("删除失败");
    }

    /**
     * @Description: 添加用户
     * @Param: dx
     * @return:
     * @Date: 2022/11/26
     */
    @Override
    public Result<CreateStaffResult> createStaff(CreateStafffParam createStafffParam) {
        log.info("添加用户信息:{}", createStafffParam);
        //缺少判断部门 参数  角色和和职称是否符合
        Staff username = staffService.getOne(new QueryWrapper<Staff>().eq("username", createStafffParam.getUsername()));
        if (username != null) throw new HisException(SmsResultCode.USERNAME_REPETITION);

        //判断科室id是否存在以及可不可以使用
        Dept byId = deptService.getById(createStafffParam.getDeptId());
        if ( byId== null ||byId.getDeptStatus().equals("OFF")) {
            throw new HisException("科室不存在 或者不可以使用",2001);
        }

        //判断挂号等级是否存在以及可不可以使用
        RegistrationRank byId1 = registrationRankService.getById(createStafffParam.getRegistrationRankId());
        if(byId == null || byId.getDeptStatus().equals("OFF")){
            throw new HisException("挂号等级不存在 或者不可以使用",2001);
        }
        //判断角色是否存在 是否可以使用
        Role byId2 = roleService.getById(createStafffParam.getRoleId());
        if(byId2 == null || byId2.getRoleStatus().equals("OFF")){
            throw new HisException("角色等级不存在 或者不可以使用",2001);
        }
        Staff staff = BeanUtil.copyProperties(createStafffParam, Staff.class);
        staff.setCreateTime(new Date());
        staff.setStaffStatus("AVAILABLE");
        staffService.save(staff);
        CreateStaffResult createStaffResult = BeanUtil.copyProperties(createStafffParam, CreateStaffResult.class);
        return Result.getInstance(CreateStaffResult.class).setData(createStaffResult).setMsg("新增用户成功");
    }
}
